package wx.wx.contorller;


import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import wx.wx.service.WxService;
import wx.wx.util.Utlis;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;


@RequestMapping
@RestController
public class DemoController extends HttpServlet {


    @GetMapping("/demo")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         * 参数	描述
         * signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
         * timestamp	时间戳
         * nonce	随机数
         * echostr	随机字符串
         */
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        //校验
        if (WxService.check(timestamp, nonce, signature)) {
            PrintWriter writer = response.getWriter();
            writer.print(echostr);
            writer.flush();
            writer.close();
            System.out.println("接入成功");
        } else {
            System.out.println("接入失败");
        }
    }

    /**
     * 接收消息事件推送
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("/demo")
    public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException {

       /* ServletInputStream is = request.getInputStream();
        byte[] b=new byte[1024];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len=is.read(b))!=-1){
            sb.append(new String(b,0,len));
        }
        System.out.println(sb.toString());*/
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        Map<String, String> requestMap = WxService.parseRequest(request.getInputStream());
        String resXml = WxService.getResponse(requestMap);
        System.out.println(resXml);
        PrintWriter writer = response.getWriter();
        writer.print(resXml);
        writer.flush();
        writer.close();
    }

    @GetMapping("/GetTicket")
    public void getTicket(HttpServletRequest request, HttpServletResponse response) {
        try {
            PrintWriter writer = response.getWriter();
            String ticket = WxService.getOrCodeTicket();
            writer.print(ticket);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 网页授权
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getUserInfo")
    public void getUserInfo(HttpServletRequest request, HttpServletResponse response) {
       //获取code
        String code = request.getParameter("code");
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        url=url.replace("APPID","wx484f75cacfb7e6e1").replace("SECRET","69a1be806e560b19313555db6217654a").replace("CODE",code);
        System.out.println(url);
        String s = Utlis.get(url);
        System.out.println(s);
        String at = JSONObject.fromObject(s).getString("access_token");
        String openid = JSONObject.fromObject(s).getString("openid");
        //拉去用户信息
        url="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        url=url.replace("ACCESS_TOKEN",at).replace("OPENID",openid);
        String s1 = Utlis.get(url);
        System.out.println(s1);
    }

    @GetMapping("/index")
    public ModelAndView test1() {
        return new ModelAndView("index");
    }

    @GetMapping("/flake")
    public ModelAndView test2() {
        return new ModelAndView("flake");
    }

}
